package com.jieyang.oa.controller.expense;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.Vo.AuditVo;
import com.jieyang.oa.pojo.entity.*;
import com.jieyang.oa.service.AuditingService;
import com.jieyang.oa.service.ExpenseItemService;
import com.jieyang.oa.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: 报销管理
 * @date 2021/8/21 11:31
 */
@WebServlet("/expenseServlet")
public class ExpenseServlet extends BaseServlet {

    @Autowired
    private ExpenseService expenseService;

    @Autowired
    private ExpenseItemService expenseItemService;

    @Autowired
    private AuditingService auditingService;

    /**
     * @Author 2927624517@qq.com
     * @Description 添加报销记录
     * @Date 2021/8/28 15:00
     * @Param [req, resp]
     * @return void
    **/
    public void expenseAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        double totalAmount = 0.0;

        String[] types = req.getParameterValues("type");
        String[] amounts = req.getParameterValues("amount");
        String[] itemDescs = req.getParameterValues("itemDesc");
        Employee emp = (Employee) req.getSession().getAttribute("emp");


        for (int i = 0; i < types.length; i++) {
            totalAmount += Double.parseDouble(amounts[i]);
        }

        String mgrId = req.getParameter("mgrId");
        String expDesc = req.getParameter("expDesc");

        Expense expense = new Expense();
        expense.setEmpId(emp.getEmpId());
        expense.setExpDesc(expDesc);
        expense.setNextAuditor(mgrId);
        expense.setTotalAmount(totalAmount);

        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        expense.setExpTime(timeStr);
        expense.setStatus("新建");
        expenseService.insert(expense);
        Long expId = expense.getExpId();

        for (int i = 0; i < types.length; i++) {
            ExpenseItem expenseItem = new ExpenseItem();
            expenseItem.setExpId(expId);
            expenseItem.setType(types[i]);
            expenseItem.setAmount(Double.valueOf(amounts[i]));
            expenseItem.setItemDesc(itemDescs[i]);

            expenseItemService.insert(expenseItem);
        }

        resp.getWriter().write("true");
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查看待审批的报销记录
     * @Date 2021/8/28 15:01
     * @Param [req, resp]
     * @return void
    **/
    public void toAudit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        Employee emp = (Employee) req.getSession().getAttribute("emp");

        String empId = emp.getEmpId();
        List<AuditVo> auditVos = expenseService.selectByAudit(empId);

        resp.getWriter().write(new Gson().toJson(auditVos));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 审批报销记录
     * @Date 2021/8/28 15:01
     * @Param [req, resp]
     * @return void
    **/
    public void audit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();

        Long expId = Long.valueOf(req.getParameter("expId"));
        String result = req.getParameter("result");
        String auditDesc = req.getParameter("auditDesc");

        Auditing auditing = new Auditing();
        auditing.setExpId(expId);
        auditing.setResult(result);
        auditing.setAuditDesc(auditDesc);
        auditing.setApprover(empId);

        Integer insert = auditingService.insert(auditing);

        resp.getWriter().write(insert+"");
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 我的报销记录
     * @Date 2021/8/28 15:02
     * @Param [req, resp]
     * @return void
    **/
    public void myExpenseList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageInfo<Expense> dutyPageInfo = expenseService.selectByEmpId(empId, pageNum, pageSize);
        resp.getWriter().write(new Gson().toJson(dutyPageInfo));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 我的具体报销项记录
     * @Date 2021/8/28 15:02
     * @Param [req, resp]
     * @return void
    **/
    public void MyExpenseListItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        String expId = req.getParameter("expId");

        List<ExpenseItem> expenseItems = expenseItemService.selectByExpId(expId);
        resp.getWriter().write(new Gson().toJson(expenseItems));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 我的审核记录
     * @Date 2021/8/28 15:03
     * @Param [req, resp]
     * @return void
    **/
    public void MyAuditList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        String expId = req.getParameter("expId");

        List<Auditing> auditings = auditingService.selectByExpId(expId);
        resp.getWriter().write(new Gson().toJson(auditings));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 我的审核历史
     * @Date 2021/8/28 15:03
     * @Param [req, resp]
     * @return void
    **/
    public void MyAuditHistoryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();

        List<Auditing> auditings = auditingService.selectByEmpId(empId);
        resp.getWriter().write(new Gson().toJson(auditings));

    }
}
